package com.atguigu.bigdata.chapter11.table_api;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.descriptors.Csv;
import org.apache.flink.table.descriptors.Json;
import org.apache.flink.table.descriptors.Kafka;
import org.apache.flink.table.descriptors.Schema;

import static org.apache.flink.table.api.Expressions.$;

// 静态导入: 可以把一个类中的静态成员直接导入, 然后在当前类中可以直接使用, 而不用添加类名

/**
 * @Author lzc
 * @Date 2022/9/9 14:09
 */
public class Flink03_Tabale_Connector_Kafka {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 2000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        // 直接从文件中读取数据
        Schema schema = new Schema()
            .field("id", DataTypes.STRING())
            .field("ts", DataTypes.BIGINT())
            .field("vc", DataTypes.INT());
        
        // 从kafka读取数据
        
        tEnv
            .connect(
                new Kafka()
                    .version("universal")
                    .property("bootstrap.servers", "hadoop162:9092,hadoop163:9092,hadoop164:9092")
                    .property("group.id", "atguigu")
                    .topic("s1")
                    .startFromLatest()
            )
            .withFormat(new Csv())
            .withSchema(schema)
            .createTemporaryTable("sensor");
        
        Table table = tEnv.from("sensor");
        
        Table result = table
            .where($("id").isEqual("sensor_1"))
            .select($("id"), $("ts"), $("vc"));
        
        
        tEnv
            .connect(
                new Kafka()
                    .version("universal")
                    .property("bootstrap.servers", "hadoop162:9092,hadoop163:9092,hadoop164:9092")
                    .topic("s2")
                    .sinkPartitionerFixed()
            )
            .withFormat(new Json())
            .withSchema(schema)
            .createTemporaryTable("sensor_out");
    
        result.executeInsert("sensor_out");
        
    }
}
